package com.jiamao.lib;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;

import android.util.DisplayMetrics;

import androidx.multidex.MultiDex;

import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseApplication;
import me.goldze.mvvmhabit.crash.CaocConfig;
import me.goldze.mvvmhabit.utils.KLog;

public class BaseLibApplication extends BaseApplication  {



    public static int screenWidth, screenHegiht;

    public String packgeName;




    @Override
    public void onCreate() {
        super.onCreate();
       // FreelineCore.init(this);
        //蒲公英内测
       // PgyCrashManager.register(getApplicationContext());
        init();

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void init() {

        packgeName = getPackageName();
       // spUtil = getInstant();

        DisplayMetrics dm = new DisplayMetrics();
        dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels; // 屏幕宽（像素，如：480px）
        screenHegiht = dm.heightPixels; // 屏幕高（像素，如：800px）
    }



    @Override
    public void onTerminate() {
        super.onTerminate();
        exit();
    }

    /**
     * 遍历所有仍驻在内存中的Activity并finish,再退出应用
     */
    private void exit() {
        AppManager.getAppManager().finishAllActivity();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * ‘
     * 重启APP
     */
    public void restApp() {
        AppManager.getAppManager().finishAllActivity();
        Intent i = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }

    /**
     *
     */
    public void startLogin() {
        //logOut();
    //    LoginActivity.startActivityNew(this);
      //  AppManager.getAppManager().finishAllActivityNotClass(LoginActivity.class);
    }


    public boolean isApkDebugable(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {

                return appProcess.processName;
            }
        }
        return null;
    }


}
